package com.yhsoft.mall.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.mall.controller.request.*;
import com.yhsoft.mall.controller.response.ModuleInfoOutVo;
import com.yhsoft.mall.entity.ModuleInfo;
import com.yhsoft.mall.mapper.ModuleInfoMapper;
import com.yhsoft.mall.service.IModuleInfoService;
import com.yhsoft.mall.util.Constants;
import com.yhsoft.sys.entity.BaicInterfaceConfig;
import com.yhsoft.sys.service.IBaicInterfaceConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 模块信息表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-08
 */
@Service
public class ModuleInfoServiceImpl extends ServiceImpl<ModuleInfoMapper, ModuleInfo> implements IModuleInfoService {

    @Resource
    IBaicInterfaceConfigService iBaicInterfaceConfigService;


    /**
     * 新增 搜索 模块内容
     *
     * @param inVo
     */
    @Override
    public void addSearchBar(SearchBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(JSONUtil.parseObj(inVo.getContent()).toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(0));

        //通过companyid查询appid
        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 搜索 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateSearchBar(SearchBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(JSONUtil.parseObj(inVo.getContent()).toString());
        this.baseMapper.updateById(moduleInfo);
    }


    /**
     * 新增 轮播图 模块内容
     *
     * @param inVo
     */
    @Override
    public void addSlideBar(SlideBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(inVo.getContent().toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(1));

        //通过companyid查询appid
        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 轮播图 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateSlideBar(SlideBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(inVo.getContent().toString());
        this.baseMapper.updateById(moduleInfo);
    }


    /**
     * 新增 公告 模块内容
     *
     * @param inVo
     */
    @Override
    public void addNoticeBar(NoticeBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(JSONUtil.parseObj(inVo.getContent()).toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(2));

        //通过companyid查询appid
        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 公告 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateNoticeBar(NoticeBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(JSONUtil.parseObj(inVo.getContent()).toString());
        this.baseMapper.updateById(moduleInfo);
    }


    /**
     * 新增 菜单 模块内容
     *
     * @param inVo
     */
    @Override
    public void addMenuBar(MenuBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(inVo.getContent().toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(3));

        //通过companyid查询appid
        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 菜单 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateMenuBar(MenuBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(inVo.getContent().toString());
        this.baseMapper.updateById(moduleInfo);
    }


    /**
     * 新增 热销商品 模块内容
     *
     * @param inVo
     */
    @Override
    public void addHotBar(HotBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(inVo.getContent().toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(4));

        //通过companyid查询appid
        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 热销商品 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateHotBar(HotBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(inVo.getContent().toString());
        this.baseMapper.updateById(moduleInfo);
    }

    /**
     * 新增 商品 模块内容
     *
     * @param inVo
     */
    @Override
    public void addGoodsBar(GoodsBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);

        moduleInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        moduleInfo.setContent(inVo.getContent().toString());
        moduleInfo.setModuleName(Constants.MODULE_NAME.get(5));

        moduleInfo.setAppId(getBaicInterfaceConfig(moduleInfo.getCompanyId()).getAppId());

        this.baseMapper.insert(moduleInfo);
    }

    /**
     * 修改 商品 模块内容
     *
     * @param inVo
     */
    @Override
    public void updateGoodsBar(GoodsBarInVo inVo) {
        ModuleInfo moduleInfo = new ModuleInfo();
        BeanUtils.copyProperties(inVo, moduleInfo);
        moduleInfo.setContent(inVo.getContent().toString());
        this.baseMapper.updateById(moduleInfo);
    }


    @Override
    public ModuleInfoOutVo getModuleInfo(String appId) {
        LambdaQueryWrapper<ModuleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModuleInfo::getIsTopic, 0);
        if (appId.isEmpty()) {
            queryWrapper.eq(ModuleInfo::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        } else {
            queryWrapper.eq(ModuleInfo::getAppId, appId);
        }

        List<ModuleInfo> moduleInfos = this.baseMapper.selectList(queryWrapper);

        ModuleInfoOutVo outVo = new ModuleInfoOutVo();
        moduleInfos.forEach(ModuleInfo -> {
            //搜索
            if (Constants.MODULE_NAME.get(0).equals(ModuleInfo.getModuleName())) {
                SearchBarInVo vo = new SearchBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //json字符串转bean
                SearchBarContentInVo contentVo = JSONUtil.toBean(ModuleInfo.getContent(), SearchBarContentInVo.class);
                vo.setContent(contentVo);
                outVo.setSearchBar(vo);
            }
            //轮播图
            if (Constants.MODULE_NAME.get(1).equals(ModuleInfo.getModuleName())) {
                SlideBarInVo vo = new SlideBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //jsonArray字符串转List<bean>
                List<SlideBarContentListInVo> contentVo = JSONUtil.toList(ModuleInfo.getContent(), SlideBarContentListInVo.class);
                vo.setContent(contentVo);
                outVo.setSlideBar(vo);
            }
            //公告
            if (Constants.MODULE_NAME.get(2).equals(ModuleInfo.getModuleName())) {
                NoticeBarInVo vo = new NoticeBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //json字符串转bean
                NoticeBarContentInVo contentVo = JSONUtil.toBean(ModuleInfo.getContent(), NoticeBarContentInVo.class);
                vo.setContent(contentVo);
                outVo.setNoticeBar(vo);
            }
            //菜单
            if (Constants.MODULE_NAME.get(3).equals(ModuleInfo.getModuleName())) {
                MenuBarInVo vo = new MenuBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //jsonArray字符串转List<bean>
                List<MenuBarContentListInVo> contentVo = JSONUtil.toList(ModuleInfo.getContent(), MenuBarContentListInVo.class);
                vo.setContent(contentVo);
                outVo.setMenuBar(vo);
            }
            //热销商品
            if (Constants.MODULE_NAME.get(4).equals(ModuleInfo.getModuleName())) {
                HotBarInVo vo = new HotBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //jsonArray字符串转List<bean>
                List<HotBarContentListInVo> contentVo = JSONUtil.toList(ModuleInfo.getContent(), HotBarContentListInVo.class);
                vo.setContent(contentVo);
                outVo.setHotBar(vo);
            }
            //商品
            if (Constants.MODULE_NAME.get(5).equals(ModuleInfo.getModuleName())) {
                GoodsBarInVo vo = new GoodsBarInVo();
                vo.setId(ModuleInfo.getId());
                vo.setIsEnable(ModuleInfo.getIsEnable());

                //json字符串转bean
                GoodsBarContentListInVo contentVo = JSONUtil.toBean(ModuleInfo.getContent(), GoodsBarContentListInVo.class);
                vo.setContent(contentVo);
                outVo.setGoodsBar(vo);
            }
        });
        return outVo;
    }

    /**
     * 通过companyId查询appid
     *
     * @param companyId
     * @return
     */
    public BaicInterfaceConfig getBaicInterfaceConfig(Integer companyId) {
        LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaicInterfaceConfig::getCompanyId, companyId);
        return iBaicInterfaceConfigService.getBaseMapper().selectOne(wrapper);
    }

    @Override
    public boolean exists(String BarName) {
        LambdaQueryWrapper<ModuleInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ModuleInfo::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        wrapper.eq(ModuleInfo::getIsTopic, 0);
        wrapper.eq(ModuleInfo::getModuleName, BarName);
        return this.getBaseMapper().exists(wrapper);
    }

}
